package com.zz.parkingsystem.controller.user;

import com.zz.parkingsystem.entity.Car;
import com.zz.parkingsystem.service.CarService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ResponseBody
@Controller
@RequestMapping("/car")
@Api(value = "",tags = "车辆接口")
public class CarController {
    @Autowired
    private CarService carService;
    //使用中
    //用户id查名下所有车辆
    @ResponseBody
    @GetMapping("/selCarByUserId/{userId}")
    public Result selCarByUserId(@PathVariable Integer userId){
        List<Car> cars = carService.selCarByUserId(userId);
        return ResultGenerator.genSuccessResult(cars);
    }
    //使用中
    //由车牌号查车辆
    @ResponseBody
    @GetMapping("/selCarByClId/{carLicensePlate}")
    public Result selCarByClId(@PathVariable(value = "carLicensePlate") String carLicensePlate){
        Car carToStay = carService.selCarBycarLicensePlate(carLicensePlate);
        if(carToStay!=null){
            return ResultGenerator.genSuccessResult(carToStay);
        }
        return ResultGenerator.genFailResult("没有查到此车牌对应的车辆，可能该用户没有注册");
    }
    //使用中
    //用户绑定车辆
    @ResponseBody
    @PostMapping("/addCar")
    public Result addCar(@RequestBody Car car){
        String message =  carService.insertSelectiveUser(car);
        if(message.equals("车辆绑定成功")){
            return ResultGenerator.genSuccessResult(message);
        }
       return ResultGenerator.genFailResult(message);
    }
    //使用中
    //用户解绑车辆
    @ResponseBody
    @GetMapping("/unBindCar/{carId}")
    public Result unBindCar(@PathVariable Integer carId){
        carService.unBindCar(carId);
        return ResultGenerator.genSuccessResult("解绑成功!");
    }
}
